import { apiService, getCancelToken } from "@/utils/rtkQuery";

/** 用户登录参数 */
export type LoginQuery = {
  /** 用户名 */
  username: string;
  /** 登录密码 */
  password: string;
  /** 自动登录 */
  autoLogin: boolean;
  /** 登录类型 */
  type: string;
};

/** 用户登录返回 */
export type LoginResponse = {
  accessToken: string;
  name: string;
  uid: string;
  avatar: string;
  access: string;
};

/** 检查登录 */
export type CheckLoginResponse = {
  name: string;
  uid: number;
  status: string;
};

/** 验证吗 */
export type FakeCaptchaQuery = {
  phone: string;
};

export interface FakeCaptchaResponse {
  code?: number
  status?: string
}

export interface UserInfoResource {
  name: string;
  avatar: number;
  email: number;
  uid: number;
  access: number;
  address: number;
  phone: number;
}

export const OperationType = {
  1: "add",// 添加
  2: "delete",// 删除
  3: "edit",// 编辑
  4: "view"// 查看
}

export type UserMenu = {
  /**
    * 唯一标识
    */
  id: number;
  /**
   * 菜单名称
   */
  name: string;
  /**
   * 图标
   */
  icon?: string;
  /**
  * 资源路由
  */
  path?: string;
  /**
   * 是否在菜单中隐藏
   */
  hiddenInMenu?: boolean;
  /**
   * 操作权限
   */
  operations?: number[];
}

export const userRest = {
  /** 登录 */
  login: "/public/login",
  /** 登出 */
  logout: "/private/logout",
  /**
   * 检查登录
   */
  checkLogin: "/private/checkLogin",
  /**
   * 验证码
   */
  captcha: "/private/captcha",
  /**
   * 用户信息
   */
  userInfo: "/private/user/info",
  /**
   * 用户权限列表
   */
  menus: "/private/user/menu",
};

const { cancelToken, sourceToken } = getCancelToken();
export { cancelToken };

const userApi = apiService
  .enhanceEndpoints({
    addTagTypes: ["UserMenu", "UserInfo"],
  })
  .injectEndpoints({
    endpoints: (build) => ({
      login: build.mutation<LoginResponse, LoginQuery>({
        query: (data) => ({
          url: userRest.login,
          method: "POST",
          cancelToken: sourceToken,
          data: data,
        }),
        invalidatesTags: ["UserMenu", "UserInfo"],
      }),
      logout: build.mutation<void, void>({
        query: () => ({
          url: userRest.logout,
          method: "POST",
        }),
      }),
      checkLogin: build.query<CheckLoginResponse, void>({
        query: () => ({
          url: userRest.checkLogin,
          method: "POST",
          cancelToken: sourceToken,
        }),
        providesTags: ["UserMenu", "UserInfo"],
      }),
      getFakeCaptcha: build.mutation<FakeCaptchaResponse, FakeCaptchaQuery>({
        query: () => ({
          url: userRest.captcha,
          method: "POST",
        }),
      }),
      getUserInfo: build.mutation<UserInfoResource, void>({
        query: () => ({
          url: userRest.userInfo,
          method: "POST",
          cancelToken: sourceToken,
        }),
      }),
      getUserMenus: build.mutation<{ menus: UserMenu[] }, void>({
        query: () => ({
          url: userRest.menus,
          method: "POST",
          cancelToken: sourceToken,
        }),
      }),
    }),
    overrideExisting: false,
  });

export const {
  useCheckLoginQuery,
  useLoginMutation,
  useLogoutMutation,
  useGetFakeCaptchaMutation,
  useGetUserInfoMutation,
  useGetUserMenusMutation,
} = userApi;
export default userApi;
